package com.cwd.arm.inter.controller;

import com.cwd.arm.application.dto.PayDto;
import com.cwd.arm.application.service.read.facade.PayReadService;
import com.cwd.arm.application.service.write.facade.PayWriteService;
import com.cwd.arm.inter.query.PayQuery;
import com.cwd.arm.inter.util.DtoToVoUtil;
import com.cwd.arm.inter.util.VoToDtoUtil;
import com.cwd.arm.inter.vo.ResponseVo;
import com.cwd.arm.inter.vo.pay.PayAddVo;
import com.cwd.arm.inter.vo.pay.PayUpdateVo;
import com.cwd.arm.inter.vo.pay.PayVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by yan on 21-11-21.
 */
@Api(value = "支出接口", description = "支出接口")
@RestController
@RequestMapping(value = "/pay")
public class PayController {

    @Autowired
    private PayReadService readService;

    @Autowired
    private PayWriteService writeService;

    @ApiOperation(value = "查询支出信息", notes = "查询收款节点信息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", required = true,dataType = "Long", paramType = "path")
    })
    @GetMapping("/{id}")
    public ResponseVo<PayVo> getReceiveNode(@PathVariable Long id) {
        PayDto dto = readService.selectById(id);
        return ResponseVo.<PayVo>BUILDER().success(DtoToVoUtil.convertToVo(dto));
    }

    @ApiOperation(value = "查询支出列表", notes = "查询支出列表")
    @GetMapping("")
    public ResponseVo<List<PayVo>> getPayList(PayQuery query) {
        return ResponseVo.<List<PayVo>>BUILDER().success(DtoToVoUtil.convertToPayVoList(readService.selectByProjectId(query.getProjectId())));
    }

    @ApiOperation(value = "新增支出", notes = "新增支出")
    @Secured({"ROLE_PAY_ADD"})
    @PostMapping("")
    public ResponseVo<String> add(@RequestBody PayAddVo addVo) {
        writeService.save(VoToDtoUtil.convertToDto(addVo));
        return ResponseVo.<String>BUILDER().success();
    }

    @ApiOperation(value = "修改支出", notes = "修改支出")
    @Secured({"ROLE_PAY_EDIT"})
    @PutMapping("")
    public ResponseVo<String> update(@RequestBody PayUpdateVo updateVo) {
        writeService.update(VoToDtoUtil.convertToDto(updateVo));
        return ResponseVo.<String>BUILDER().success();
    }

    @ApiOperation(value = "删除支出", notes = "删除支出")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "支出id",required = true,dataType = "Long", paramType = "path")
    })
    @Secured({"ROLE_PAY_DEL"})
    @DeleteMapping("/{id}")
    public ResponseVo<String> del(@PathVariable Long id) {
        writeService.del(id);
        return ResponseVo.<String>BUILDER().success();
    }
}
